package com.googlecode.jpocketmod.actions;

import com.googlecode.jpocketmod.DialogManager;
import com.googlecode.jpocketmod.state.AppState;
import com.googlecode.jpocketmod.templates.PocketModTemplate;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.Action;

/**
 *
 * @author David Sosby
 */
public class LoadTemplateAction extends ApplicationAction  {
	public LoadTemplateAction()  {
		this.putValue(Action.NAME, "Load Template");
		this.putValue(Action.SHORT_DESCRIPTION, "Load a JPocketMod Template");
		this.putValue(Action.LONG_DESCRIPTION, "Load a JPocketMod Template");
		this.putValue(Action.ACTION_COMMAND_KEY, "l");
	}

	public void actionPerformed(ActionEvent e) {
		runAction();
	}

	@Override
	protected void runAction() {
		File templateFile = DialogManager.getInstance().showLoadTemplateDialog();
		
		try  {
			PocketModTemplate pmt = PocketModTemplate.loadFromFile(templateFile);
		
			System.out.println("Loading template: " + templateFile);
		
			AppState.getInstance().setCurrentlyLoadedTemplate(pmt);
			fireTemplateLoadedEvent();	/*Tell anyone that wants to know*/
		} catch (Exception ex)  {
			//TODO: Dialog notify an error occurred
			System.err.println("Error loading template: " + ex);
		}
	}
}
